import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';
import 'package:flutter_ahj_app/base/values/dimens.dart';
import 'package:permission_handler/permission_handler.dart';



typedef void PermissionCallback(String data);

class PerUtils {
  // static const platform = const MethodChannel("com.qingning.qn/checkPermission"); //分析1
  static checkPermission(BuildContext context, List<Permission> permission,
      {Key key, PermissionCallback callback,String destion,String titleName}) async {
    try {
      // Map<String, dynamic> map = Map();
      // map["permission"] = list;
       String result = "1";
       bool isPermission=true;//是不是获取权限

       PermissionStatus permissionStatus=PermissionStatus.granted;//权限没有被获取

       for(var i=0;i<permission.length;i++){
         if (Platform.isIOS) {
           PermissionStatus statuses = await permission[i].request();
         }
        //
         if(isPermission){
           isPermission= Platform.isIOS ? await permission[i].status.isPermanentlyDenied : await permission[i].status.isDenied;
         }
       }
       // if (Platform.isIOS) {
       //   isPermission = false;
       // }

      if (isPermission) {
        //权限未获得
        Map<Permission, PermissionStatus> statuses = await permission.request();

        for(var i=0;i<permission.length;i++){
          if(statuses[permission[i]]==PermissionStatus.permanentlyDenied){//不再提示
            permissionStatus= PermissionStatus.permanentlyDenied;
          }else if(statuses[permission[i]]==PermissionStatus.denied){//没有权限
            permissionStatus=PermissionStatus.denied;
          }
        }

        if (permissionStatus == PermissionStatus.denied) {
          if(Platform.isAndroid&&permission[0]==Permission.notification){//通知权限
            openAppSettings();
          }

          result = "0";
          // 没有权限
          if (callback != null) callback(result);

        }else if(permissionStatus == PermissionStatus.granted){
          result = "1";
          if (callback != null) callback("1");
        }

        else if(permissionStatus == PermissionStatus.permanentlyDenied){//不再提示
          result = "0";
          showDialog(
            context: context,
            builder: (context) {
              return StatefulBuilder(
                builder: (context, mSetState) {
                  return AlertDialog(
                    title: Text(titleName??"权限申请失败",
                        style: TextStyle(fontSize: Dimens.textSize32)),
                    content: Text(
                      destion??"您拒绝了我们必要的一些权限，请在设置中授权,否则功能无法正常使用！",
                      style: TextStyle(
                          color: SQColor.black_666666,
                          fontSize: Dimens.textSize28),
                    ),
                    actions: <Widget>[
                      new FlatButton(
                          child: new Text("去设置"),
                          onPressed: () async {
                            // 去给权限
                            Navigator.of(context).pop();
                            openAppSettings();
                          }),
                      new FlatButton(
                          child: new Text("取消"),
                          onPressed: () {
                            Navigator.of(context).pop();
                          }),
                    ],
                  );
                },
              );
            },
          );
          
        }
      }else{
        result = "1";
      }
      

      // 请求权限方法，map的key=permission value=List<Integer>
//      final String result = await platform.invokeMethod('openSetting',map); // 打开设置页面方法
//      result == 0 没权限  result == 1 有权限  result == 2 没有权限并且勾选不再提示，需要弹窗提示打开设置授权
//       if (result == "0") {
//         // 没有权限
//         if (callback != null) callback("0");
//       }
//       if (result == "1") {
//         // 有权限
//         if (callback != null) callback("1");
//       }
//       if (result == "2") {
//         // 没有权限，并且勾选不再显示，弹窗提示去设置
//
//       }
      return result;
    } on PlatformException catch (e) {
      // 没有权限
      if (callback != null) callback("0");
      return "0";
    }
  }
}
